require "nvm"

-- 初始化nvm获取配置参数
nvm.init("config.lua", true)
local limitChangeFlag = false

local blueLimit, yellowLimit, orangeLimit, redLimit, topic, eqrTopic,
      registSource, groupIdNvm, clientIdNvm, clientNo = tonumber(nvm.get(
                                                                     "blueLimit")),
                                                        tonumber(
                                                            nvm.get(
                                                                "yellowLimit")),
                                                        tonumber(
                                                            nvm.get(
                                                                "orangeLimit")),
                                                        tonumber(
                                                            nvm.get("redLimit")),
                                                        nvm.get("centerConfig")[11],
                                                        nvm.get("centerConfig")[12],
                                                        nvm.get("registSource"),
                                                        nvm.get("centerConfig")[5],
                                                        nvm.get("centerConfig")[6],
                                                        nvm.get("info")[1]
local function initConfigParam()
    if registSource == "1" then
        if not string.find(topic, ",") then
            log.info("设置预警主题！！！")
            topic =
                topic .. "," .. topic .. "/" .. groupIdNvm .. "," .. topic ..
                    "/" .. clientIdNvm
            nvm.sett("centerConfig", 11, topic, "SVR")
            limitChangeFlag = true
        end
        if not string.find(eqrTopic, ",") then
            log.info("设置速报主题！！！！")
            eqrTopic =
                eqrTopic .. "," .. eqrTopic .. "/" .. groupIdNvm .. "," ..
                    eqrTopic .. "/" .. clientIdNvm
            nvm.sett("centerConfig", 12, eqrTopic, "SVR")
            limitChangeFlag = true
        end
    end
    if blueLimit < yellowLimit and yellowLimit < orangeLimit and orangeLimit <
        redLimit then
        if blueLimit <= 0 or blueLimit == nil or blueLimit == "" then
            nvm.set("blueLimit", "1")
            limitChangeFlag = true
        end
        if yellowLimit <= 0 or yellowLimit == nil or yellowLimit == "" then
            nvm.set("yellowLimit", "3")
            limitChangeFlag = true
        end
        if orangeLimit <= 0 or orangeLimit == nil or orangeLimit == "" then
            nvm.set("orangeLimit", "5")
            limitChangeFlag = true
        end
        if redLimit <= 0 or redLimit == nil or redLimit == "" then
            nvm.set("redLimit", "7")
            limitChangeFlag = true
        end
    else
        nvm.set("blueLimit", "1")
        nvm.set("yellowLimit", "3")
        nvm.set("orangeLimit", "5")
        nvm.set("redLimit", "7")
        limitChangeFlag = true
    end

    if limitChangeFlag then
        sys.restart("颜色下限值改为默认，重启生效！")
    end
end
sys.taskInit(initConfigParam)
